package sunmi.payment.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.TextView;

import java.util.Locale;

import mi.payment.R;

/**
 * @author Lee64 on 2017/2/23.
 */
public class LoadDialog extends Dialog {

    private TextView msgView;
    private String msgText;

    public LoadDialog(Context context, String text) {
        this(context, sunmi.sunmiui.R.style.defaultDialogStyle, text);
    }

    private LoadDialog(Context context, int theme, String text) {
        super(context, theme);
        init(text);
    }

    private void init(String msg) {
        setContentView(R.layout.dialog_loading);
        msgView = (TextView) findViewById(R.id.tv_msg);
        if (getWindow() != null) {
            // 居中
            getWindow().getAttributes().gravity = Gravity.CENTER;
        }
        // 点击空白不取消
        setCanceledOnTouchOutside(false);
        // 点击返回按钮不取消
        setCancelable(false);
        setMessage(msg);
    }

    public void setMessage(String msg) {
        msgText = TextUtils.isEmpty(msgText) ? getContext().getString(R.string.base_loadding) : msg;
        msgView.setText(msgText);
    }

    public void updateProgress(int progress) {
        String format = String.format(Locale.getDefault(), "%s(%ds)", msgText, progress);
        msgView.setText(format);
    }


}
